//
//  LBPageItemView.m
//  LBPageControl
//
//  Created by liubo on 2023/6/10.
//

#import "LBPageItemView.h"
#import "LBPageItemView.h"

static NSInteger const pageControlMargin = 1;

@interface LBPageItemView ()


@end

@implementation LBPageItemView

{
    CGFloat normalRed;
    CGFloat normalGreen;
    CGFloat normalBlue;
    CGFloat selectedRed;
    CGFloat selectedGreen;
    CGFloat selectedBlue;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor.whiteColor colorWithAlphaComponent:0.5];
        self.layer.cornerRadius = 1.5;
        self.clipsToBounds = YES;
    }
    return self;
}

- (void)setIsCurrentPage:(BOOL)isCurrentPage
{
    _isCurrentPage = isCurrentPage;
    if (isCurrentPage) {
        self.backgroundColor = self.selectedColor;
    } else {
        self.backgroundColor = self.normalColor;
    }
}

- (void)setNormalColor:(UIColor *)normalColor
{
    _normalColor = normalColor;
    NSArray *array = [self getRGBWithColor:normalColor];
    normalRed = [array[0] floatValue];
    normalGreen = [array[1] floatValue];
    normalBlue = [array[2] floatValue];
}

- (void)setSelectedColor:(UIColor *)selectedColor
{
    _selectedColor = selectedColor;
    NSArray *array = [self getRGBWithColor:selectedColor];
    selectedRed = [array[0] floatValue];
    selectedGreen = [array[1] floatValue];
    selectedBlue = [array[2] floatValue];
}

- (void)configWithState
{
    if (self.isCurrentPage) {
        self.bounds = CGRectMake(0, 0, 12, 3);
    }else{
        self.bounds = CGRectMake(0, 0, 7, 3);
    }
}
- (void)annimationWithState:(NSInteger)index
{
    CGFloat leftPadding = 0;
    CGFloat itemWidth = 7;
    if (self.pageIndex == index) {
        leftPadding = 8 *self.pageIndex+pageControlMargin*self.pageIndex;
        itemWidth = 12;
    } else if (self.pageIndex>index){
        leftPadding =  8 *self.pageIndex + 6+pageControlMargin*self.pageIndex;
        itemWidth = 7;
    } else {
        leftPadding = 8 *self.pageIndex+pageControlMargin*self.pageIndex;
        itemWidth = 7;
    }
    
    [UIView animateWithDuration:0.35 delay:0 options:UIViewAnimationOptionCurveLinear animations:^{
        self.frame = CGRectMake(leftPadding, 7, itemWidth, 3);
    } completion:^(BOOL finished) {
        
    }];
    
}


- (void)scroProgressWithState:(NSInteger)index
{
//    NSLog(@"滚到第几---%ld----向左滚动---%d",index,self.isLeftScro);
    
    CGFloat itemWidth = 7;
    CGFloat variableWidth = 5;
    CGFloat itemHeight = 3;
    CGFloat itemMargin = 3;
    
    CGFloat sumPadding = 0;
    
    CGFloat itemWidthMu = variableWidth*(1-self.scroProgress);
    
    CGFloat itemWidthMuNext = variableWidth*self.scroProgress;

    CGFloat itemWidthZ =0;
       
    sumPadding = itemWidth*self.pageIndex+itemMargin*self.pageIndex;

    if (self.isLeftScro) {
        NSInteger nextItem = index-1;
        if (self.pageIndex<nextItem) {
            sumPadding = sumPadding;
            itemWidthZ = itemWidth;
        } else if(self.pageIndex==nextItem){
            [self tool_color:(1-self.scroProgress)];
            sumPadding = sumPadding;
            itemWidthZ = itemWidth+itemWidthMuNext;
        } else if(self.pageIndex==index){
            [self tool_color:self.scroProgress];
            sumPadding = sumPadding+itemWidthMuNext;
            itemWidthZ = itemWidth+itemWidthMu;
        } else {
            sumPadding = sumPadding+itemWidthMu+itemWidthMuNext;
            itemWidthZ = itemWidth;
        }
    } else {
        NSInteger nextItem = index+1;
        if (self.pageIndex>nextItem) {
            sumPadding = sumPadding+itemWidthMu+itemWidthMuNext;
            itemWidthZ = itemWidth;
        }else if(self.pageIndex==nextItem){
            [self tool_color:(1-self.scroProgress)];
            sumPadding = sumPadding+itemWidthMu;
            itemWidthZ = itemWidth+itemWidthMuNext;
            
        }else if(self.pageIndex==index){
            [self tool_color:self.scroProgress];
            sumPadding = sumPadding;
            itemWidthZ = itemWidth+itemWidthMu;
        }else{
            sumPadding = sumPadding;
            itemWidthZ = itemWidth;
        }
    }
    
    self.frame = CGRectMake(sumPadding, 7, itemWidthZ, itemHeight);
}

-(void)tool_color:(float )scroProgress {
    //204/255   0.8
    //153/255   0.6
    //51
    UIColor *topColor = [UIColor colorWithRed:(selectedRed + (1 - normalRed)*scroProgress) green:(selectedGreen +(1 - normalGreen)*scroProgress) blue:(selectedBlue +(1 - normalBlue)*scroProgress) alpha:1];
    self.backgroundColor = topColor;
}

- (NSArray *)getRGBWithColor:(UIColor *)color
{
    CGFloat red = 0.0;
    CGFloat green = 0.0;
    CGFloat blue = 0.0;
    CGFloat alpha = 0.0;
    [color getRed:&red green:&green blue:&blue alpha:&alpha];
    return @[@(red), @(green), @(blue), @(alpha)];
}

@end
